﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using CustomControlDatabaseInterface.View;
using CustomControlDatabaseInterface.ViewModel;

namespace CustomControlDatabaseInterface
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        static App()
        {
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            LoginWindow loginWindow = new LoginWindow();
            var loginViewModel = new LoginViewModel();

            EventHandler handler = null; 
            handler = delegate
            {
                loginViewModel.RequestClose -= handler; 
                loginWindow.Close();
            };
            loginViewModel.RequestClose += handler; 

            EventHandler loginHandler = null;
            loginHandler = delegate
            {
                loginViewModel.LoginEvent -= loginHandler;
                StartMainWindow();
                loginWindow.Close();
            };
            loginViewModel.LoginEvent += loginHandler;
            loginWindow.DataContext = loginViewModel;

            loginWindow.Show(); 

        }

        private void StartMainWindow()
        {
            MainWindow window = new MainWindow();
            var viewModel = new MainWindowViewModel();

            EventHandler handler = null;
            handler = delegate
            {
                viewModel.RequestClose -= handler;
                window.Close();
            };
            viewModel.RequestClose += handler;

            window.DataContext = viewModel;

            window.Show(); 
        }
    }
}
